from typing import *
from leetcode.editor.common.node import *


# leetcode submit region begin(Prohibit modification and deletion)
class Solution(object):
    def findMaxLength(self, nums):
        n = len(nums)
        pre_nums= [0]*(n+1)
        for i in range(1,len(pre_nums)):
            pre_nums[i] = pre_nums[i-1] + self.zero(nums[i-1])
        val_to_index = {}
        res = 0
        for i in range(len(pre_nums)):
            if pre_nums[i]  not in val_to_index:
                val_to_index[pre_nums[i]] = i
            else:
                res = max(res, i-val_to_index[pre_nums[i]])
        return res


    def zero(self,n):
        if n==0:
            return -1
        return n
# leetcode submit region end(Prohibit modification and deletion)


if __name__ == '__main__':
    solution = Solution()
    print(solution.findMaxLength([0,1]))
    # your test code here